#include "StdAfx.h"
#include "Tape.h"


#if ( NORMAL_MODE )

Tape::Tape( int* x, int* y, std::string op )
{
	m_TotalLength = 0;

	Make( x, y, op );
}


Tape::~Tape( void )
{
	m_Data.clear();
}


void Tape::Show( State::Enum current_state, relative_type current_head, relative_type current_step )
{
	std::string show( m_Data );


	current_head -= 1;

	if( State::Halt != current_state )
	{
		if( current_head < (relative_type)m_Data.size() && 0 <= current_head )
		{
			show.insert( current_head, "[" );
			show.insert( current_head + 2, "]" );
		}	
	}

	std::ostringstream ostr;
	ostr << current_step;
	show.append( "  (Step : " );
	show.append( ostr.str().c_str() );
	show.append( ")" );
	
	for( size_t i = m_Data.size(); i < m_TotalLength; ++i )
	{
		show.append( " " );
	}	
	show.insert( 0, "\r" );


	std::cout << show;

	show.clear();
}


int Tape::FindFirst( int* src )
{
	for( int i = 0; i < MAX_BIT_SIZE; ++i )
	{
		if( src[i] )
		{
			return i;
		}
	}

	return 0;
}


void Tape::Push( int val )
{
	std::ostringstream ostr;
	ostr << val;

	m_Data.append( ostr.str().c_str() );
}


void Tape::Make( int* x, int* y, std::string op )
{
	for( int i = FindFirst( x ); i < MAX_BIT_SIZE; ++i )
	{
		Push( x[i] );		
	}

	m_Data.append( op );

	for( int i = FindFirst( y ); i < MAX_BIT_SIZE; ++i )
	{
		Push( y[i] );
	}

	m_TotalLength = m_Data.size();
}

#else

Tape::Tape( std::string x, std::string y, std::string op )
{
	m_Data.assign( x );
	m_Data.append( op );
	m_Data.append( y );
}


Tape::~Tape( void )
{
}


void Tape::Show( State::Enum current_state, relative_type current_head, relative_type current_step )
{
	std::string show( m_Data );


	current_head -= 1;

	if( State::Halt != current_state )
	{
		if( current_head < (relative_type)m_Data.size() && 0 <= current_head )
		{
			show.insert( current_head, "[" );
			show.insert( current_head + 2, "]" );
		}	
	}

	std::ostringstream ostr;
	ostr << current_step;
	show.append( "  (Step : " );
	show.append( ostr.str().c_str() );
	show.append( ")" );
	
	system( "cls" );


	std::cout << show;

	show.clear();
}

#endif